package com.knowledgepoint.study.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.knowledgepoint.study.common.api.CommonPage;
import com.knowledgepoint.study.common.api.CommonResult;
import com.knowledgepoint.study.model.entity.KnowledgePointCategoryLevelOne;
import com.knowledgepoint.study.service.KnowledgePointCategoryLevelOneService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 一级知识点分类管理
 */
@RestController
@RequestMapping("/knowledge/category/one")
@Api(tags = "一级知识点分类管理")
public class KnowledgePointCategoryLevelOneController {

    @Autowired
    private KnowledgePointCategoryLevelOneService categoryService;

    @ApiOperation(value = "创建一级分类", notes = "创建一个新的一级知识点分类")
    @ApiResponses({
        @ApiResponse(code = 200, message = "创建成功"),
        @ApiResponse(code = 400, message = "参数错误"),
        @ApiResponse(code = 500, message = "系统内部错误")
    })
    @PostMapping("/create")
    public CommonResult<Void> create(
            @ApiParam(value = "一级分类信息", required = true)
            @Validated @RequestBody KnowledgePointCategoryLevelOne category) {
        if (categoryService.isNameExist(category.getName(), null)) {
            return CommonResult.failed("该分类名称已存在");
        }
        boolean success = categoryService.add(category);
        return success ? CommonResult.success(null, "创建成功") : CommonResult.failed("创建失败");
    }

    @ApiOperation(value = "修改一级分类", notes = "根据ID修改一级知识点分类信息")
    @ApiResponses({
        @ApiResponse(code = 200, message = "操作成功"),
        @ApiResponse(code = 400, message = "参数错误"),
        @ApiResponse(code = 404, message = "资源不存在"),
        @ApiResponse(code = 500, message = "系统内部错误")
    })
    @PostMapping("/update/{id}")
    public CommonResult<Void> update(
            @ApiParam(value = "一级分类ID", required = true) @PathVariable Integer id,
            @ApiParam(value = "一级分类信息", required = true) @Validated @RequestBody KnowledgePointCategoryLevelOne category) {
        category.setId(id);
        if (categoryService.isNameExist(category.getName(), id)) {
            return CommonResult.failed("该分类名称已存在");
        }
        boolean success = categoryService.update(category);
        return success ? CommonResult.success(null, "修改成功") : CommonResult.failed("修改失败");
    }

    @ApiOperation(value = "删除一级分类", notes = "根据ID删除一级知识点分类")
    @ApiResponses({
        @ApiResponse(code = 200, message = "删除成功"),
        @ApiResponse(code = 400, message = "参数错误"),
        @ApiResponse(code = 404, message = "资源不存在"),
        @ApiResponse(code = 500, message = "系统内部错误")
    })
    @PostMapping("/delete/{id}")
    public CommonResult<Void> delete(
            @ApiParam(value = "一级分类ID", required = true) @PathVariable Integer id) {
        // TODO: 检查该分类下是否有二级分类，有则不允许删除
        boolean success = categoryService.removeById(id);
        return success ? CommonResult.success(null, "删除成功") : CommonResult.failed("删除失败");
    }

    @ApiOperation(value = "分页查询一级分类", notes = "根据关键字分页查询一级知识点分类列表")
    @ApiResponses({
        @ApiResponse(code = 200, message = "查询成功"),
        @ApiResponse(code = 500, message = "系统内部错误")
    })
    @GetMapping("/list")
    public CommonResult<CommonPage<KnowledgePointCategoryLevelOne>> list(
            @ApiParam(value = "搜索关键字") @RequestParam(value = "keyword", required = false) String keyword,
            @ApiParam(value = "页码", defaultValue = "1") @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页数量", defaultValue = "10") @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<KnowledgePointCategoryLevelOne> page = categoryService.page(
                new Page<>(pageNum, pageSize),
                new LambdaQueryWrapper<KnowledgePointCategoryLevelOne>()
                        .like(keyword != null, KnowledgePointCategoryLevelOne::getName, keyword)
                        .or()
                        .like(keyword != null, KnowledgePointCategoryLevelOne::getDescription, keyword)
                        .orderByAsc(KnowledgePointCategoryLevelOne::getSortOrder)
        );
        return CommonResult.success(CommonPage.restPage(page));
    }

    @ApiOperation(value = "获取所有一级分类", notes = "获取所有一级知识点分类列表")
    @ApiResponses({
        @ApiResponse(code = 200, message = "查询成功"),
        @ApiResponse(code = 500, message = "系统内部错误")
    })
    @GetMapping("/list/all")
    public CommonResult<List<KnowledgePointCategoryLevelOne>> listAll() {
        List<KnowledgePointCategoryLevelOne> list = categoryService.listAll();
        return CommonResult.success(list);
    }

    @ApiOperation(value = "根据ID获取一级分类详情", notes = "根据ID获取一级知识点分类的详细信息")
    @ApiResponses({
        @ApiResponse(code = 200, message = "查询成功"),
        @ApiResponse(code = 404, message = "资源不存在"),
        @ApiResponse(code = 500, message = "系统内部错误")
    })
    @GetMapping("/{id}")
    public CommonResult<KnowledgePointCategoryLevelOne> getItem(
            @ApiParam(value = "一级分类ID", required = true) @PathVariable Integer id) {
        KnowledgePointCategoryLevelOne category = categoryService.getDetailById(id);
        return CommonResult.success(category);
    }
}
